package Prova::Controller::Accessor;

use strict;
use warnings;
use base 'Catalyst::Controller';
use Data::Dumper;

=head1 NAME

Prova::Controller::Accessor - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 default

default action

=cut

sub default : Private {
    my ( $self, $c ) = @_;

    $c->forward('list');
}

=head2 list

list action

=cut

sub list : Private {
    my ( $self, $c ) = @_;

    $c->stash->{template} = 'accessor/list.tt';

    my $accessors = [ $c->model('DBIC::Accessor')->search( {}, { order_by => 'data_block_id' } ) ];
    $c->stash->{accessors} = $accessors;
}

=head2 delete

delete action

=cut

sub delete : Local {
    my ( $self, $c ) = @_;

    my $model = $c->model('DBIC::Accessor')->find( id => $c->req->args->[0] );
    $model->delete() if defined $model;
    $c->res->redirect('/accessor');
}

=head2 edit

edit action

=cut

sub edit : Local {
    my ( $self, $c ) = @_;

    $c->stash->{template} = 'accessor/edit.tt';

    my $id = $c->req->args->[0] || $c->req->param('id');

    my $model = $c->model('DBIC::Accessor')->find( id => $id );

    my $method = $c->request->method;
    $c->log->debug( '===== method : ' . $method );

#    if ( defined $model ) {
        if ( $method eq 'POST' ) {
            $model->name( $c->req->param('name') );
            $model->alias( $c->req->param('alias') );
            $model->update();
            $c->res->redirect('/accessor');
        } else {
            my $hash;
            $hash->{id} = $model->id;
            $hash->{name} = $model->name;
            $hash->{alias} = $model->alias;
            $c->req->params($hash);
        }
#    }
}

=head2 create

internal create action

=cut

sub create : Private {
    my ( $self, $c ) = @_;

    my $datablock = $c->stash->{datablock};
    my $data      = $c->stash->{accessor};

    # delete
    my $accessors = $c->forward( 'find_by_id', [ ( $datablock->id ) ] );
    if ( ref $accessors eq 'ARRAY' ) {
        for my $accessor (@$accessors) {
            $c->log->info( "delete accessor : id=" . $accessor->id );
            $accessor->delete;
        }
    }

    # create
    if ( ref $data eq 'HASH' ) {
        my @new;
        for ( sort { $a <=> $b } keys %$data ) {
            $data->{$_}->{data_block_id} = $datablock->id;
            $c->log->debug( "===== create accessor : " . Dumper $data->{$_} );
            my $accessor = $c->model('DBIC::Accessor')->create( $data->{$_} );
            push @new, $accessor;
            $c->log->info( "create accessor : id=" . $accessor->id );
        }
        $c->stash->{accessor} = \@new;
    }
}

=head2 find_by_id

get accessor models having datablock_id

=cut

sub find_by_id : Private {
    my ( $self, $c, $datablock_id ) = @_;

    my $accessor = [ $c->model('DBIC::Accessor')->search( { data_block_id => $datablock_id } ) ];

    return $accessor;
}

=head2 get_testcase_accessors

get accessor models having testcase_id

=cut

sub get_testcase_accessors : Private {
    my ( $self, $c, $testcase_id ) = @_;

    my @datablocks = $c->model('DBIC::DataBlock')->search(
        { test_case_id => $testcase_id }
    );
    return unless scalar @datablocks;

    my @accessors = $c->model('DBIC::Accessor')->search(
        { data_block_id => [ map { $_->id } @datablocks ] },
        { order_by => 'alias' }
    );
    return unless scalar @accessors;

    $c->stash->{accessors} = \@accessors if @accessors;
}

=head1 AUTHOR

Shanon Inc, E<lt>http://www.shanon.co.jp/E<gt>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
